package com.vhall.component.entity.interactive.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @author yongzheng.xu
 * @date 2021/5/15 17:25
 */
@Data
public class QuestionPage<T extends Serializable> implements Serializable {

    @JsonProperty(value = "from")
    private Integer from;

    @JsonProperty(value = "to")
    private Integer to;

    @JsonProperty(value = "total")
    private Integer total;

    @JsonProperty(value = "page")
    private Integer currentPage;

    @JsonProperty(value = "last_page")
    private Integer lastPage;

    @JsonProperty(value = "per_page")
    private Integer perPage;

    @JsonProperty(value = "list")
    private List<T> data;

    public QuestionPage(Integer index, int size, int total, List<T> data) {
        this.currentPage = index;
        this.total = total;
        this.perPage = size;
        this.data = data;
        this.from = CollectionUtils.isNotEmpty(data) ? (index - 1) * (size + 1) : 0;
        this.to = CollectionUtils.isNotEmpty(data) ? this.from + (data.size() - 1) : 0;
        this.lastPage = Math.max((int) Math.ceil(total / (double) size), 1);
    }

}
